package cn.hou.socket01._01demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

//客户端
public class Client {

    final static String ADDRESS = "127.0.0.1";//要连接的服务端ip
    final static int PORT = 9527;

    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            socket = new Socket(ADDRESS, PORT);//创建客户端socket对象
            //接受服务端数据流,与发送数据流
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);//true,自动冲刷,不用等到数据多的时候
            //向服务器端发送数据,一旦发送数据,服务端就会收到并作出响应,但是服务端必须先启动并处于阻塞状态
            out.println("向服务端发送数据:{...}");
            //获取服务端返回的数据
            System.out.println("客户端接受到的数据: " + in.readLine());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
